<?php
    require_once '../../common/DBHelper.php';

    // 连接数据库
    $conn = conn();
    // sql语句
    $sql = "SELECT posts.id,categories.`name`,posts.title,users.nickname,
posts.created,posts.content,posts.feature,posts.views,posts.likes,posts.category_id,
(SELECT count(id) FROM comments WHERE post_id = posts.id) as commentsCount 
FROM posts
LEFT JOIN categories ON posts.category_id = categories.id
left JOIN users ON users.id = posts.user_id
 ORDER BY created DESC LIMIT 5";
    $res = query($conn, $sql);
    // 如果有数据，直接把数据返回即可
    $response = ['code' => 0,'msg' => '操作失败'];
    
    if ($res) {
        $response['code'] = 1;
        $response['msg'] = '操作成功';
        $response['data'] = $res;
    }
    // 返回给前端
    echo json_encode($response, JSON_UNESCAPED_UNICODE);
